const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        erCode: '/images/scanning.png',
        selectIcon: '/images/select_icon.png',
        equipment_number: '', //主板编号
        mainboardType: [], //获取到的主板主板类型
        mainboard_type_number: 999, //选中的主板主板类型
        mainboard_type_index: "",
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this;
        that.getMainboardType();
    },

    //编辑主板编号
    editNumber(e) {
        this.setData({
            equipment_number: e.detail.value
        })
    },
    
    //保存主板信息
    addLibrary() {
        var that = this;
        var url = 'Partner/Equipment/addDevicelibrary';
        if (that.data.equipment_number == '') {
            app.showToast('请输入主板编号');
            return false;
        } else if (that.data.mainboard_type_number == '') {
            app.showToast("请选择主板类型");
            return false;
        }
        app.ajax({
            url: url,
            data: {
                equipment_number: that.data.equipment_number,
                mainboard_type_number: that.data.mainboard_type_number,
            },
            success: function (res) {
                if (res.data.code == 1000) {
                    app.showToast(res.data.message, "success");
                    setTimeout(() => {
                        const pages = getCurrentPages();
                        if (pages.length >= 2) {
                            const traget = pages[pages.length - 2];
                            traget.reloadData();
                        }
                        wx.navigateBack();
                    }, 2000)
                } else {
                    app.showToast(res.data.message)
                }
            }
        })
    },
    //  主板类型选择
    mainboardChange: function (e) {
        var that = this;
        this.setData({
            mainboard_type_index: e.detail.value,
            mainboard_type_number: that.data.mainboardType[e.detail.value]['number']
        });
    },
    // 扫描二维码
    scan: function () {
        app.scanDeviceCode().then(res => {
            this.setData({
                equipment_number: res
            })
        }).catch(error => {
            app.showToast(error)
        })
    },
    //获取主板主板类型
    getMainboardType: function () {
        var that = this;
        app.ajax({
            url: 'Common/Common/getEquipmentMainboard',
            method: "POST",
            data: {},
            success: function (res) {
                if (res.data.code == 1000) {
                    var data = res.data.data;
                    that.setData({
                        mainboardType: data
                    })
                } else {
                    app.showToast(res.data.message);
                }
            }
        })
    },
})